const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    key:"", //搜索关键字
    sortType: 1, //排序
    page:1,
    activityList:[],
    loadingStatus:false,
    msgStatus:false,
    test:function(){
      return 1;
    }
  },
  // 排序
  sortList(e){
    let that = this;
    let sortType = e.target.dataset.type;
    let thatSortType = that.data.sortType;
    that.setData({
      msgStatus: false
    })
    if (sortType == 1 || sortType == 2){
      if (sortType == thatSortType){
        wx.hideLoading();
        return;
      }else{
        that.setData({
          sortType: sortType
        })
      }
    } else if (sortType == 3){
      if (thatSortType == 3){
        that.setData({
          sortType:4
        })
      } else {
        that.setData({
          sortType: 3
        })
      }
    }
    wx.showLoading({
      title: '排序中...',
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data: {
        page: 1,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            activityList: res.data.data,
            page: 1
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 加载更多
  load(){
    let that = this;
    if (that.data.loadingStatus) { return; }
    if (that.data.msgStatus) { return; }
    that.setData({
      loadingStatus: true
    })
    let page = that.data.page;
    page = ++page;
    wx.showLoading({
      title: '正在加载更多',
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data: {
        page: page,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              msgStatus:true
            })
          } else {
            that.setData({
              activityList: that.data.activityList.concat(res.data.data),
              msgStatus: false
            })
          }
        } else {
          page = --page;
          app.showToast(res.data.msg)
        }
        that.setData({
          page: page,
          loadingStatus: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      key: options.key || ""
    })
    wx.showLoading({
      title: '加载列表...',
    })
    wx.request({
      url: app.buildUrl('/search/get-lists'),
      header: app.buildHeader(),
      data:{
        page: 1,
        search_word: that.data.key,
        type: that.data.sortType
      },
      success(res){
        wx.hideLoading();
        if (res.data.code == 200){
          that.setData({
            activityList: res.data.data
          })
        }else{
          app.showToast(res.data.msg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  }
})